﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.ParagraphProperties;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunProperties;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table;
using TP = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table.Properties;
using TCP = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table.CellProperties;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Samples.Word
{
    class Tables
    {
        public static void Create()
        {
            Document doc = DocumentManager.Instance.CreateWordDocument();
            Body body = doc.CreateBody();

            Table table = body.AddTable(3, 2);
            table.AddProperty(new TP.Width { Value = 7000 });

            table.Grid = new TableGrid();
            table.Grid.AddGridColumn(2000);
            table.Grid.AddGridColumn(3000);
            table.Grid.AddGridColumn(2000);

            table[0][0].AddParagraph().AddRun().AddSimpleText("Hello");

            for (int i = 0; i < 2; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    table[i][j].CellProperty = new TableCellProperties();
                    table[i][j].CellProperty.Properties.Add(new TCP.Borders()
                    {
                        Top = new TCP.BorderDefinition() { Value = ST_Border.Single },
                        Left = new TCP.BorderDefinition() { Value = ST_Border.Single },
                        Bottom = new TCP.BorderDefinition() { Value = ST_Border.Single },
                        Right = new TCP.BorderDefinition() { Value = ST_Border.Single }
                    });
                }
            }

            Paragraph p1 = table[0][1].AddParagraph();
            p1.AddProperty(new ParagraphAlignment() { Value = ST_Jc.Center });
            Run r1 = p1.AddRun();
            r1.AddProperty(new Bold());
            r1.AddSimpleText("World");

            table[0][2].AddParagraph().AddRun().AddSimpleText("!");

            table[1][0].AddParagraph().AddRun().AddSimpleText("");

            Table innerTable = table[1][1].AddTable(2, 1);
            for (int j = 0; j < 2; ++j)
            {
                innerTable[0][j].CellProperty = new TableCellProperties();
                innerTable[0][j].CellProperty.Properties.Add(new TCP.Borders()
                {
                    Top = new TCP.BorderDefinition() { Value = ST_Border.Single },
                    Left = new TCP.BorderDefinition() { Value = ST_Border.Single },
                    Bottom = new TCP.BorderDefinition() { Value = ST_Border.Single },
                    Right = new TCP.BorderDefinition() { Value = ST_Border.Single }
                });
            }
            innerTable[0][0].AddParagraph().AddRun().AddSimpleText("Inner");
            innerTable[0][1].AddParagraph().AddRun().AddSimpleText("table");
            table[1][1].AddParagraph().AddRun().AddSimpleText("");

            table[1][2].AddParagraph().AddRun().AddSimpleText("");
            
            doc.Save(@"samples\table.docx");

            Document parsed = null;
            if (DocumentManager.Instance.OpenWordDocument(@"samples\table.docx", out parsed))
            {
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
